﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JOIN.Rights.Model;
using Kevin.Framework.Data;
using System.Data;

namespace JOIN.Rights.BLL
{
    public class CompanyBLL : BLLBase<CompanyInfo>, ISelect<CompanyInfo>
    {
        /// <summary>
		/// ActionBLL  构造函数  
		/// </summary>
        public CompanyBLL(CompanyInfo entity)
			:base(entity)
		{
		
		}
		/// <summary>
		/// ActionBLL  构造函数  
		/// </summary>
		/// <param name="operationMode">操作模式</param>
        /// <param name="jsonEntity">前台javascript传过来的JSON实体</param>
        public CompanyBLL(string operationMode, string jsonEntity)
        {
            OperState cmd = (OperState)Enum.Parse(typeof(OperState), operationMode);
            CompanyInfo entity = ConvertJSON2Entity(jsonEntity);
            base.OperState = cmd;
            base.Entity = entity;
            base.ServiceResult = new ServiceResult<CompanyInfo>();
            base.ServiceResult.Successed = true;
            base.ServiceResult.Object = base.Entity;
        }

        public ServiceResult<CompanyInfo> SelectByProperties(int pageSize, int pageIndex)
        {
            this.ServiceResult = new ServiceResult<CompanyInfo>();
            try
            {
                int recordCount = 0;
                List<CompanyInfo> list = base.Entity.SelectedByProperties<CompanyInfo>(out recordCount, pageSize, pageIndex);

                ServiceResult.Successed = true;
                ServiceResult.RecordCount = recordCount;
                ServiceResult.List = list;

            }
            catch (Exception ex)
            {
                ServiceResult.ErrorMsg = "数据查询失败:" + ex.Message;
                ServiceResult.Successed = false;
                ServiceResult.RecordCount = 0;
                ServiceResult.List = null;
            }

            return ServiceResult;
        }

        public DataTable SelectAllCompany()
        {
            DataTable result = base.Entity.SelectedByProperties();
            return result;
        }

        #region

        protected override bool PreSaveProcess()
        {
            if (base.OperState == BLL.OperState.Add)
            {
                CompanyInfo entity = new CompanyInfo();
                entity.CompanyID = base.Entity.CompanyID;
                if (entity.SelectedByProperties().Rows.Count > 0)
                {
                    this.ServiceResult = new ServiceResult<CompanyInfo>();
                    ServiceResult.Successed = false;
                    ServiceResult.ErrorMsg = "企业：" + Entity.CompanyID + " 已经存在！";
                    ServiceResult.Object = this.Entity;
                    return false;
                }
                base.Entity.ID = Guid.NewGuid().ToString();
            }
            return base.PreSaveProcess();
        }

        #endregion
    }
}
